package com.maple.common.widget

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import com.maple.common.R
import com.maple.common.databinding.WidgetSettingViewBinding
import com.maple.common.ext.drawable
import com.maple.common.ext.dpToPx
import androidx.core.content.withStyledAttributes

/**
 * Tip:设置页面
 * Create by SeVen on 2024/3/21 9:42
 */
class SettingView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    FrameLayout(context, attrs, defStyleAttr) {

    private val binding: WidgetSettingViewBinding

    init {
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        binding = WidgetSettingViewBinding.inflate(inflater, this, true)

        initAttrs(context, attrs)
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        attrs?.let {
            context.withStyledAttributes(attrs, R.styleable.SettingView) {
                for (i in 0 until indexCount) {
                    when (val attr = getIndex(i)) {
                        R.styleable.SettingView_middleText -> {
                            val resId = getString(attr)
                            binding.tvMiddle.text = resId
                        }

                        R.styleable.SettingView_middleTextColor -> {
                            val color = getColor(attr, 0)
                            binding.tvMiddle.setTextColor(color)
                        }

                        R.styleable.SettingView_middleTextSize -> {
                            val size = getDimension(attr, 0f)
                            binding.tvMiddle.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
                        }

                        R.styleable.SettingView_middleTextBold -> {
                            val bold = getBoolean(attr, false)
                            val paint = binding.tvMiddle.paint
                            paint.isFakeBoldText = bold
                        }

                        R.styleable.SettingView_leftIcon -> {
                            val drawable = getDrawable(attr)
                            binding.ivLeftIcon.setImageDrawable(drawable)
                        }

                        R.styleable.SettingView_rightIcon -> {
                            val drawable = getDrawable(attr)
                            binding.ivRightIcon.setImageDrawable(drawable)
                        }

                        R.styleable.SettingView_leftVisible -> {
                            val show = getBoolean(attr, true)
                            val visible = if (show) VISIBLE else GONE
                            binding.ivLeftIcon.visibility = visible
                        }

                        R.styleable.SettingView_rightVisible -> {
                            val show = getBoolean(attr, true)
                            val visible = if (show) VISIBLE else GONE
                            binding.llRightLayer.visibility = visible
                            binding.ivRightIcon.visibility = visible
                        }

                        R.styleable.SettingView_showTopDividerLine -> {
                            val show = getBoolean(attr, false)
                            val visible = if (show) VISIBLE else GONE
                            binding.dividerTopLine.visibility = visible
                        }

                        R.styleable.SettingView_showBottomDividerLine -> {
                            val show = getBoolean(attr, true)
                            val visible = if (show) VISIBLE else GONE
                            binding.dividerBottomLine.visibility = visible
                        }

                        R.styleable.SettingView_background -> {
                            val color = getColor(attr, 0)
                            binding.root.setBackgroundColor(color)
                        }
                    }
                }
            }
        }
    }

    /**
     * 设置中间内容
     */
    fun setMiddleText(middleText: String) {
        binding.tvMiddle.text = middleText
    }

    /**
     * 获取中间内容
     */
    fun getMiddleText(): String {
        return binding.tvMiddle.text.toString()
    }

    /**
     * 设置中间文字颜色
     */
    fun setMiddleTextColor(@ColorInt color: Int) {
        binding.tvMiddle.setTextColor(color)
    }

    /**
     * 设置中间文字大小
     */
    fun setMiddleTextSize(size: Float) {
        binding.tvMiddle.setTextSize(TypedValue.COMPLEX_UNIT_PX, dpToPx(size))
    }

    /**
     * 是否显示顶部分割线
     */
    fun showTopDividerLine(isShow: Boolean) {
        val visible = if (isShow) VISIBLE else GONE
        binding.dividerTopLine.visibility = visible
    }

    /**
     * 是否显示顶部分割线
     */
    fun showBottomDividerLine(isShow: Boolean) {
        val visible = if (isShow) VISIBLE else GONE
        binding.dividerBottomLine.visibility = visible
    }

    /**
     * 设置背景颜色
     */
    fun setBackground(@ColorInt color: Int) {
        binding.root.setBackgroundColor(color)
    }

    fun setRightIcon(@DrawableRes resId: Int) {
        binding.ivRightIcon.setImageDrawable(context.drawable(resId))
    }

    fun setLeftVisible(show: Boolean) {
        val visible = if (show) VISIBLE else GONE
        binding.ivLeftIcon.visibility = visible
    }

    fun setRightVisible(show: Boolean) {
        val visible = if (show) VISIBLE else GONE
        binding.llRightLayer.visibility = visible
        binding.ivRightIcon.visibility = visible
    }
}